package com.konngmou.springCache.service.impl;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.konngmou.springCache.dao.PersonRepository;
import com.konngmou.springCache.domain.Person;
import com.konngmou.springCache.service.PersonService;

@Service
public class PersonServiceImpl implements PersonService{
	
	@Resource
	private PersonRepository personRepository;

	@CachePut(value="person", key="#person.id")
	public Person save(Person person) {
		Person p = personRepository.save(person);
		System.out.println("为id、key为:"+p.getId()+"数据做了缓存");
		return p;
	}

	@CacheEvict(value="person")
	public void remove(Long id) {
		System.out.println("删除了id、key为"+id+"的数据缓存");
		//这里不做实际删除操作
		//personRepository.deleteById(id);
		
	}

	@Cacheable(value="person", key="#person.id")
	public Person findOne(Person person) {
		Optional<Person> p = personRepository.findById(person.getId());
		
		if(null != p) {
			System.out.println("为id、key为:"+p.get().getId()+"数据做了缓存");
			return p.get();
		}
		return null;
	}

}
